package com.example.amtf_tmdrecycle;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Todolistinfo> projectList = new ArrayList<>();

    private ProjectAdapter adapter;

    private RecyclerView recyclerView;

    private Button addbtn_main;

    private int position_current;   //在修改progressbar时获取item

    private MyDatabaseHelper dbHelper;

    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        this.deleteDatabase("TodoList.db");
        dbHelper =new MyDatabaseHelper(this,"TodoList.db",null,1);
        dbHelper.getWritableDatabase();
        db=dbHelper.getWritableDatabase();

        recyclerView = findViewById(R.id.list_view);

        addbtn_main = findViewById(R.id.add_button_main);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);      //设置item的方向
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        adapter = new ProjectAdapter(projectList);                        //设置适配器
        recyclerView.setAdapter(adapter);

        /**
         * 实现拖拽与滑动删除
         * **/
        ItemTouchHelperCallback helperCallback = new ItemTouchHelperCallback(adapter);          //将ItemTouchHelper与recyclerView绑定
        helperCallback.setSwipeEnable(true);
        helperCallback.setDragEnable(true);
        ItemTouchHelper helper = new ItemTouchHelper(helperCallback);
        helper.attachToRecyclerView(recyclerView);

        addbtn_main.setOnClickListener(new View.OnClickListener() {              //按下添加按钮
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, additemActivity.class);
                startActivityForResult(intent, 1);
            }
        });

        adapter.setOnItemClickLitener(new ProjectAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View v, int position) {
                position_current=position;
                Intent intent=new Intent(MainActivity.this,edititemActivity.class);
                startActivityForResult(intent,2);
            }
        });
        //从数据库读取
        Cursor cursor=db.query("TodoList",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                String start_time=cursor.getString(cursor.getColumnIndex("time_start"));
                String end_time=cursor.getString(cursor.getColumnIndex("time_end"));
                String project=cursor.getString(cursor.getColumnIndex("project"));
                int progress=cursor.getInt(cursor.getColumnIndex("progress"));
                byte[] image=cursor.getBlob(cursor.getColumnIndex("image"));
                Todolistinfo info=new Todolistinfo(project,start_time,end_time,progress,image);
                projectList.add(info);
            }while (cursor.moveToNext());
        }
        if(!projectList.isEmpty())
        {
            Collections.sort(projectList);
            adapter.update(projectList);
        }
        cursor.close();
    }

    /**
     *得到返回的项目名称和地址
     **/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {                  //添加item
            Todolistinfo info = (Todolistinfo) data.getSerializableExtra("todolist1");
            // Log.d("MainActivity",info.getName());
            projectList.add(info);

            Collections.sort(projectList);          //添加后进行排序 实现item按从早到晚排序

            adapter.update(projectList);
            // Log.d("MainActivity","12123123");
        }
        if(requestCode==2&&resultCode==RESULT_OK){                          //修改进度
            projectList.get(position_current).setProgress(Integer.parseInt(data.getStringExtra("currentPro")));
            ContentValues values=new ContentValues();
            values.put("progress",Integer.parseInt(data.getStringExtra("currentPro")));
            db.update("TodoList",values,"project=?",new String[]{projectList.get(position_current).getName()});
            adapter.update(projectList);
        }
    }
}
